package org.wxd.media.api.record;

import org.wxd.common.utils.Assert;

import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by wangxd on 2016/11/1.
 */
public class MediaUploadRequest {

    private String name;
    private String contentType;
    private String checksum;
    private long expireMillis;
    private Map<String, String> moreInfo = Collections.emptyMap();
    private InputStream stream;

    MediaUploadRequest() {

    }

    MediaUploadRequest(Builder builder) {
        this.name = builder.name;
        this.contentType = builder.contentType;
        this.expireMillis = builder.expireMillis;
        this.moreInfo = builder.moreInfo;
        this.stream = builder.stream;
        this.checksum = builder.checksum;
    }

    public String name() {
        return name;
    }

    public String checksum() {
        return checksum;
    }

    public String contentType() {
        return contentType;
    }


    public long expireMillis() {
        return expireMillis;
    }

    public Map<String, String> moreInfo() {
        return moreInfo;
    }

    public InputStream stream() {
        return stream;
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {
        InputStream stream;
        String name;
        String checksum;
        String contentType;
        long expireMillis;
        Map<String, String> moreInfo = Collections.emptyMap();

        public MediaUploadRequest build() {
            return new MediaUploadRequest(this);
        }

        public Builder stream(InputStream stream) {
            Assert.notNull(stream, "stream must not null.");
            this.stream = stream;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder checksum(String checksum) {
            this.checksum = checksum;
            return this;
        }

        public Builder contentType(String contentType) {
            this.contentType = contentType;
            return this;
        }


        public Builder moreInfo(Map<String, String> moreInfo) {
            Assert.notNull(moreInfo, "metadata must not be null.");
            this.moreInfo = moreInfo;
            return this;
        }


        public Builder expirePersist() {
            this.expireAfterMillis(-1L);
            return this;
        }

        public Builder expireAfterDays(long days) {
            this.expireAfterMillis(TimeUnit.DAYS.toMillis(days));
            return this;
        }

        public Builder expireAfterHours(long hours) {
            this.expireAfterMillis(TimeUnit.HOURS.toMillis(hours));
            return this;
        }

        public Builder expireAfterMinutes(long minutes) {
            this.expireAfterMillis(TimeUnit.MINUTES.toMillis(minutes));
            return this;
        }

        public Builder expireAfterSeconds(long seconds) {
            this.expireAfterMillis(TimeUnit.SECONDS.toMillis(seconds));
            return this;
        }

        public Builder expireAfterMillis(long millis) {
            this.expireMillis = millis;
            return this;
        }

    }
}
